package thread;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class SallTicket extends Thread{
    private List<Ticket> lists;
    private JTextArea ta;
    public SallTicket( int count) {
        lists=new ArrayList<>();
        for (int i=0;i<count;i++){
            lists.add(new Ticket(i+1));
        }
    }

    public SallTicket(List<Ticket> lists, JTextArea ta) {
        this.lists = lists;
        this.ta = ta;
    }

    @Override
    public void run() {
        int c=0;
        while (true){
            Ticket t;
            synchronized (lists) {//加锁
                if (lists.size()<=0) break;
                t=lists.remove(0);
                c++;
            }
            ta.append(Thread.currentThread().getName()+"卖了"+t+"\n");
            System.out.println(Thread.currentThread().getName()+"卖了"+t);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ta.append(Thread.currentThread().getName()+"卖了"+c+"张！"+"\n");
    }
}
